﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace PizzaLunch
{
    public class TweetAccounts : ObservableCollection<TweetAccount>
    {
        /*public TweetAccount FindDuplicatedAccount(string SourceID, string ID)
        {
            foreach (TweetAccount tac in Items)
            {
                if (tac.Source.ID == SourceID && tac.AccountInfo.ID == ID)
                    return tac;
            }
            return null;
        }*/
    }

    public class TweetAccount : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion

        public bool Valid
        {
            get
            {
                if (AccountCfg == null)
                    return false;
                if (!string.IsNullOrEmpty(AccountCfg.accessKey) && !string.IsNullOrEmpty(AccountCfg.accessSecrect))
                    return true;
                else
                    return false;
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override bool Equals(System.Object obj)
        {
            if (obj == null)
                return false;

            TweetAccount tac = obj as TweetAccount;
            if ((System.Object)tac == null)
                return false;

            if (tac.Source.ID == Source.ID && tac.ID == ID)
                return true;
            return false;
        }

        private TweetSource _source;
        public TweetSource Source { get { return _source; } }

        //private string _name;
        //public string Name { get{return _name;} }

        public Util.AccountConfig AccountCfg { get; set; }

        public bool isDefault
        {
            get
            {
                return AccountCfg.isDefault;
            }
            set
            {
                AccountCfg.isDefault = value;
                OnPropertyChanged("isDefault");
            }
        }

        private UserInfo _accountInfo;
        public UserInfo AccountInfo { get { return _accountInfo; } }

        public long LastTimeStamp { get; set; }
        private UserInfo.DynaInfo _dyna;
        public UserInfo.DynaInfo Dyna { get { return _dyna; } }
        //public long _lastComposeStamp { get; set; }
        //public string _lastComposeID { get; set; }

        public string ID { get { return AccountInfo.GetID(this); } }

        public TweetAccount(TweetSource ts)
        {
            _source = ts;
            _accountInfo = new UserInfo(this);
            AccountCfg = new Util.AccountConfig();
            AccountCfg.sourceID = ts.ID;
            _dyna = new UserInfo.DynaInfo();
        }

        public void Query()
        {
            if (!Valid)
                return;
            Source.Query(this);
        }

        public bool Login()
        {
            if (Valid)
                return true;
            string key, secrect;
            if (Source.Login(out key, out secrect))
            {
                AccountCfg.accessKey = key;
                AccountCfg.accessSecrect = secrect;
                GetUserInfo();
                OnPropertyChanged("Valid");
                return true;
            }
            return false;
        }

        public void GetUserInfo()
        {
            Source.GetUserInfo(this);
            OnPropertyChanged("AccountInfo");
        }

        public void GetUserInfoByID(UserInfo user)
        {
            Source.GetUserInfoByID(this, user);
        }

        public void GetComments(TweetItem ti)
        {
            Source.GetComments(this, ti);
        }

        public bool Comment(TweetItem ti, string Content)
        {
            return Source.Comment(this, ti, Content);
        }

        public bool Retweet(TweetItem ti, string Content)
        {
            return Source.Retweet(this, ti, Content);
        }

        public void GetFansList(UserInfo userInfo, UserInfo.UserType uType,int index)
        {
            Source.GetFansList(this, userInfo, uType,index);
        }

        public void QueryUserTimeline(UserInfo userInfo)
        {
            Source.QueryUserTimeline(this, userInfo);
        }

        public bool Delete(TweetItem ti)
        {
            return Source.DeleteTweet(this, ti);
        }
    }
}
